﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellTalkProxy_h
#define CellTalkProxy_h

#include "CellPrerequisites.h"
#include "CellInetAddress.h"
#include "CellInet4Address.h"
#include "CellInet6Address.h"
#include "CellMessageHandler.h"
#include "CellPrimitive.h"

namespace cell
{
/** \addtogroup Core
 *  @{
 */
/** \addtogroup General
 *  @{
 */

class TalkProxyConnectorHandler;

/** Talk 服务代理。
@author
	Jiangwei Xu
*/
class _CellPrivate TalkProxy : public GeneralAlloc
{
public:
	/**
	*/
	TalkProxy(void);

	/**
	*/
	virtual ~TalkProxy(void);

public:
	/**
	*/
	void addHostAddress(const InetAddress& address);
	/**
	*/
	void removeHostAddress(const InetAddress& address);

	/**
	*/
	bool isWorking(void) const;

	/**
	*/
	void startup(void);

	/**
	*/
	void shutdown(void);

	/**
	*/
	void requestCellet(Session* session, const String& source, const String& identifier);

	/**
	*/
	void dialogueCellet(Session* session, const String& source, const Primitive& primitive);

private:
	/** 委托代理方。
	*/
	class _CellPrivate Principal : GeneralAlloc
	{

	};

	/**
	*/
	void connectPrincipal(const InetAddress& address);

private:
	typedef HashMap<BlockingConnector*, TalkProxyConnectorHandler*> Connectors;
	Connectors mConnectors;

	Inet4AddressList mV4AddressList;
	Inet6AddressList mV6AddressList;
};


/**
*/
class _CellPrivate TalkProxyConnectorHandler : public MessageHandler, public GeneralAlloc
{
public:
	/**
	*/
	TalkProxyConnectorHandler(TalkProxy* proxy);

	/**
	*/
	virtual ~TalkProxyConnectorHandler(void);

public:
	/** 创建连接会话。
	*/
	virtual void sessionCreated(Session* session) = 0;

	/** 销毁连接会话。
	*/
	virtual void sessionDestroyed(Session* session) = 0;

	/** 开启连接会话。
	*/
	virtual void sessionOpened(Session* session) = 0;

	/** 关闭连接会话。
	*/
	virtual void sessionClosed(Session* session) = 0;

	/** 接收到消息。
	*/
	virtual void messageReceived(Session* session, Message* message) = 0;

	/** 消息已发送。
	*/
	virtual void messageSent(Session* session, Message* message) = 0;

	/** 发生程序错误。
	*/
	virtual void errorOccurred(uint16 errorCode, Session* session) = 0;
};

/** @} */
/** @} */
} // end namespace cell

#endif // CellTalkProxy_h
